<template>
	<view>
		<u-navbar title="群管理"></u-navbar>
		<view class="group u-m-t-20 u-border-bottom">
			<view class="label">
				<text class="u-font-32">群内互加好友</text>
			</view>
			<view class="content u-text-right">
				<u-switch :value="info.isAddFriend" size="40" @change="changeAddFriend"></u-switch>
			</view>
		</view>
		<view class="group u-border-bottom">
			<view class="label">
				<text class="u-font-32">允许邀请成员</text>
			</view>
			<view class="content u-text-right">
				<u-switch :value="info.isInvite" size="40" @change="changeInvite"></u-switch>
			</view>
		</view>
		<view class="group u-border-bottom">
			<view class="label">
				<text class="u-font-32">成员撤回消息</text>
			</view>
			<view class="content u-text-right">
				<u-switch :value="info.isRecallMsg" size="40" @change="changeRecallMsg"></u-switch>
			</view>
		</view>
		<view class="group u-border-bottom">
			<view class="label">
				<text class="u-font-32">显示已读未读</text>
			</view>
			<view class="content u-text-right">
				<u-switch :value="info.isShowRead" size="40" @change="changeShowRead"></u-switch>
			</view>
		</view>
		<view class="group u-border-bottom">
			<view class="label">
				<text class="u-font-32">发送名片</text>
			</view>
			<view class="content u-text-right">
				<u-switch :value="info.isSendCard" size="40" @change="changeSendCard"></u-switch>
			</view>
		</view>
		<view class="group u-border-bottom">
			<view class="label">
				<text class="u-font-32">发送文件</text>
			</view>
			<view class="content u-text-right">
				<u-switch :value="info.isSendFile" size="40" @change="changeSendFile"></u-switch>
			</view>
		</view>
		<!-- <view class="group u-border-bottom">
			<view class="label">
				<text class="u-font-32">发送普通红包</text>
			</view>
			<view class="content u-text-right">
				<u-switch :value="info.isSendRedPacket" size="40" @change="changeSendRed"></u-switch>
			</view>
		</view>
		<view class="group u-border-bottom">
			<view class="label">
				<text class="u-font-32">发送扫雷红包</text>
			</view>
			<view class="content u-text-right">
				<u-switch :value="info.isShowRedMine" size="40" @change="changeShowMine"></u-switch>
			</view>
		</view> -->
		<view class="group u-border-bottom u-m-t-20">
			<view class="label">
				<text class="u-font-32">全体禁言</text>
			</view>
			<view class="content u-text-right">
				<u-switch :value="info.isSilent" size="40" @change="changeSilent"></u-switch>
			</view>
		</view>
		<navigator url="/pages/group/admin/mute" class="group u-border-bottom">
			<view class="label">
				<text class="u-font-32">禁言名单</text>
			</view>
			<view class="content u-text-right">
				<!-- <text class="u-font-32 text-gray">0人</text> -->
			</view>
			<view class="arrow">
				<text class="iconfont icon-arrow-right text-gray"></text>
			</view>
		</navigator>
		<view class="group u-border-bottom" @tap="changeMsgSaveTime">
			<view class="label">
				<text class="u-font-32">消息保留</text>
			</view>
			<view class="content u-text-right">
				<text class="u-font-32 text-gray">{{info.msgSaveTime|saveTimeF}}</text>
			</view>
			<view class="arrow">
				<text class="iconfont icon-arrow-right text-gray"></text>
			</view>
		</view>
		<navigator v-if="isMaster" url="/pages/group/admin/manager" class="group u-border-bottom u-m-t-20">
			<view class="label">
				<text class="u-font-32">管理员</text>
			</view>
			<view class="content u-text-right">
			</view>
			<view class="arrow">
				<text class="iconfont icon-arrow-right text-gray"></text>
			</view>
		</navigator>
		<u-select 
			v-model="selector.show" 
			:list="selector.list" 
			:defaultValue="selector.def"
			@confirm="resolve"
			@cancel="reject"
		></u-select>
	</view>
</template>

<script>
	import {state} from "../bus.js"
	import {
		editGroupAddFriendsState,
		editGroupInviteAuditState,
		editGroupInviteState,
		editGroupSilentState
	} from "@/api/addr.js"
	import {
		setSaveTime,
		setShowRead,
		setShowRedMine,
		setSendRedPacket,
		setSendCard,
		setSendFile,
		timeList,
		setRecallMsg
	} from "@/api/group.js"
	export default {
		data() {
			return {
				checked:false,
				selector:{
					show:false,
					def:[0],
					list:[]
				},
				bridge:null,
				lock:false
			}
		},
		computed:{
			info(){
				return state.info
			},
			isMaster(){
				return state.power.vest=='群主'
			}
		},
		filters:{
			saveTimeF(val){
				const temp = timeList.find((item)=>item.value==val)
				return temp?temp.label:''
			}
		},
		methods: {
			async changeAddFriend(){
				await editGroupAddFriendsState({
					id:this.info.id,
					state:!this.info.isAddFriend
				})
				state.info.isAddFriend = !this.info.isAddFriend
			},
			async changeInvite(){
				await editGroupInviteState({
					id:this.info.id,
					state:!this.info.isInvite
				})
				state.info.isInvite = !this.info.isInvite
			},
			async changeInviteAuth(){
				await editGroupInviteAuditState({
					id:this.info.id,
					state:!this.info.isInviteAuth
				})
				state.info.isInviteAuth = !this.info.isInviteAuth
			},
			async changeSilent(){
				await editGroupSilentState({
					id:this.info.id,
					state:!this.info.isSilent
				})
				state.info.isSilent = !this.info.isSilent
			},
			async changeRecallMsg(){
				if(this.lock){
					throw new Error('正在请求中')
				}
				try{
					this.lock = true
					const res = await setRecallMsg({
						id:this.info.id,
						state:state.info.isRecallMsg?0:1
					})
					if(res){
						state.info.isRecallMsg = !this.info.isRecallMsg
					}
				}finally{
					this.lock = false
				}
			},
			async changeShowRead(){
				if(this.lock){
					throw new Error('正在请求中')
				}
				try{
					this.lock = true
					const res = await setShowRead({
						id:this.info.id,
						state:state.info.isShowRead?0:1
					})
					if(res){
						state.info.isShowRead = !this.info.isShowRead
					}
				}finally{
					this.lock = false
				}
			},
			async changeShowMine(){
				if(this.lock){
					throw new Error('正在请求中')
				}
				try{
					this.lock = true
					const res = await setShowRedMine({
						id:this.info.id,
						state:state.info.isShowRedMine?0:1
					})
					if(res){
						state.info.isShowRedMine = !this.info.isShowRedMine
					}
				}finally{
					this.lock = false
				}
			},
			async changeSendCard(){
				if(this.lock){
					throw new Error('正在请求中')
				}
				try{
					this.lock = true
					const res = await setSendCard({
						id:this.info.id,
						state:state.info.isSendCard?0:1
					})
					if(res){
						state.info.isSendCard = !this.info.isSendCard
					}
				}finally{
					this.lock = false
				}
			},
			async changeSendFile(){
				if(this.lock){
					throw new Error('正在请求中')
				}
				try{
					this.lock = true
					const res = await setSendFile({
						id:this.info.id,
						state:state.info.isSendFile?0:1
					})
					if(res){
						state.info.isSendFile = !this.info.isSendFile
					}
				}finally{
					this.lock = false
				}
			},
			async changeSendRed(){
				if(this.lock){
					throw new Error('正在请求中')
				}
				try{
					this.lock = true
					const res = await setSendRedPacket({
						id:this.info.id,
						state:state.info.isSendRedPacket?0:1
					})
					if(res){
						state.info.isSendRedPacket = !this.info.isSendRedPacket
					}
				}finally{
					this.lock = false
				}
			},
			
			async changeMsgSaveTime(){
				if(this.lock){
					throw new Error('正在请求中')
				}
				const [{value}] = await new Promise((resolve,reject)=>{
					this.bridge = {
						resolve,
						reject
					}
					this.selector.show = true
					this.selector.list = timeList
					this.selector.def = [0]
				})
				try{
					this.lock = true
					const res = await setSaveTime({
						id:state.info.id,
						time:value
					})
					if(res){
						state.info.msgSaveTime = value
					}
				}finally{
					this.lock = false
				}
			},
			
			resolve(e){
				this.bridge&&this.bridge.resolve&&this.bridge.resolve(e)
			},
			reject(e){
				this.bridge&&this.bridge.reject&&this.bridge.reject(e)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.group{
		display: flex;
		align-items: center;
		background-color: #ffffff;
		height: 100rpx;
		padding:  0 30rpx;
		.label{
			flex-shrink: 0;
		}
		.content{
			flex: 1;
		}
		.arrow{
			flex-shrink: 0;
			margin-left: 20rpx;
		}
	}
</style>
